(function() {
	// 游表单搜索
	$('form.search-form').each(function() {
		var form = $(this);
		var keywords = form.find('input[name=keywords]');
		var placeholder = keywords.data('placeholder');
		form.find('.sub').unbind().click(function(e) {
			e.preventDefault();
			var val = $.trim(keywords.val());
			if (val == '' || val == placeholder) {
				keywords.focus();
				return false;
			}
			form.submit();
		});
	});
	var J_order = $("#J_order");
	var J_orderInput = J_order.find('input[name=order]');
	J_order.find('a[rel]').on('click', function(e) {
		e.preventDefault();
		J_orderInput.val($(this).attr('rel'));
		J_order.submit();
	});

	// 游记列表的排序
	var notes_form = $("#notes_news_nav");
	var orderInput = notes_form.find('input[name=order]');
	var orderPage = notes_form.find('input[name=currPageNo]');
	notes_form.find('a[rel]').on('click', function(e) {
		e.preventDefault();
		orderInput.val($(this).attr('rel'));
		orderPage.val('1');
		notes_form.submit();
	});

	var $J_citySelect;
	var $selectedEl = null;
	var citySelect = {
		isShow : false,
		show : function() {
			if (this.isShow) {
				return false;
			}
			this.isShow = true;
			$J_citySelect.css({
				marginTop : 20,
				opacity : 0
			}).show().animate({
				marginTop : 0,
				opacity : 1
			});
		},
		hide : function() {
			this.isShow = false;
			$J_citySelect.animate({
				marginTop : 20,
				opacity : 0
			}, function() {
				$(this).hide();
			});
		}
	};
	$("#J_filterCont,#J_secondFiletr").find('[data-local]').on('click',
			function(e) {
				e.preventDefault();
				e.stopPropagation();
				$J_citySelect = $(this).parents('.rows').find('.cityselect');
				citySelect.show();
				$selectedEl = $(this);
			});
	$('.cityselect').on(
			'click',
			'a',
			function(e) {
				e.stopPropagation();
				var self = $(this);
				if ($(this).hasClass('close')) {
					citySelect.hide();
				} else if ($(this).attr('data-city')) {
					$selectedEl.find('span').html($(this).html());
					$(this).parents('.rows:first').find('input[name=city]')
							.val($(this).attr('data-city'));
					citySelect.hide();
					self.trigger('jump', $(this).attr('data-city'));
				}
			});
	$('.cityselect').on('click', function(e) {
		e.stopPropagation();
	});
	$(document).on('click', function() {
		citySelect.isShow && citySelect.hide();
	});

	// 酒店、攻略4个部分切换
	var $J_filterNav = $("#J_filterNav");
	if ($J_filterNav.length) {
		var $J_filterCont = $J_filterNav.next();
		$J_filterNav.find('>li').on(
				'click',
				function(e) {
					e.preventDefault();
					if (!$(this).find('a').hasClass('cur')) {
						$J_filterNav.find('.cur').removeClass('cur');
						$(this).find('a').addClass('cur');
						var k = $J_filterNav.find('>li').index($(this));
						$J_filterCont.find('>div').hide().eq(k).show();

						var input = $J_filterCont.find('>div').eq(k).find(
								'.filter-text[placeholder]');
						if (!input.data('initPlaceholder')) {
							input.placeholder({
								className : 'filter-text-placeholder'
							}).on('focusin', function() {
								$(this).addClass('filter-text-focusin');
							}).on('focusout', function() {
								$(this).removeClass('filter-text-focusin');
							});
						}

					}
				});
		var k = $J_filterNav.find('a').index($J_filterNav.find('.cur'));
		$J_filterCont.find('>div').hide().eq(k).show();
		$J_filterCont.find('>div').eq(k).find('.filter-text[placeholder]')
				.placeholder({
					className : 'filter-text-placeholder'
				}).on('focusin', function() {
					$(this).addClass('filter-text-focusin');
				}).on('focusout', function() {
					$(this).removeClass('filter-text-focusin');
				});
		$J_filterCont.find('input[name=keywords]').each(function(index, item) {
			if (index !== k) {
				$(this).val('');
			}
		});
	}
	$("#J_secondFiletr>input").placeholder({
		className : 'filter-text-placeholder'
	}).on('focusin', function() {
		$(this).addClass('filter-text-focusin');
	}).on('focusout', function() {
		$(this).removeClass('filter-text-focusin');
	});
	// slider分页 和 切换
	var $sliderList = $('.slider');
	$sliderList.length
			&& $('.slider').each(
					function() {
						var $slider = $(this);
						var $sliderNav = $slider
								.find('.slider-nav > .slider-nav-li');
						var $sliderCont = $slider.find('.slider-item');

						var getPageHtml = function(moreLink) {
							var pageLen = Math.ceil($sliderCont.filter(
									':visible').find('li').length / 4);
							var html = '';
							var i = pageLen - 1;
							while (i >= 0) {
								html = '<a ' + (i == 0 ? 'class="cur"' : '')
										+ ' href="javascript:void(0)">'
										+ (i + 1) + '</a>' + html;
								i--;
							}
							// return html + '<a class="more" href="' + moreLink
							// + '">更多&gt;</a>';
							return html;
						};
						window.NS.public.tab($sliderNav, $slider
								.find('.slider-item'), 'cur',
								'slider-item-cur', $slider
										.find('>.slider-prev'), $slider
										.find('>.slider-next'), function() {
									// 由于内容于程序实现，暂屏蔽
									var link = $(this).find('a').attr('href');
									$slider.find('.slider-page').html(
											getPageHtml(link));
								});
						var link = $sliderNav.eq(0).find('a').attr('href');
						$slider.find('.slider-page').html(getPageHtml(link));
						$slider.find('.slider-page').on(
								'click',
								'a[class!=more]',
								function(e) {
									e.preventDefault();
									var pageCont = $slider
											.find('.slider-item:visible');
									var ul = pageCont.find('ul');
									var pageHeight = pageCont.height();
									if (!$(this).hasClass('cur')) {
										var page = ($(this).html() | 0) - 1;
										pageCont.animate({
											scrollTop : page * pageHeight
										}, 300);
										$(this).addClass('cur')
												.siblings('.cur').removeClass(
														'cur');
									}
								});
					});
})();